<template>
    <!--头部-->
    <div class="header">
        <div class="box1 clearfix">
            <!-- 这里是logo -->
            <div class="logo">
                <router-link to="/home">
                    <img src="@/assets/img/logo.png" />
                </router-link>
            </div>
            <!--这里是搜索和推荐栏-->
            <div class="box2">
                <div class="search clearfix">
                    <!--这里是搜索框对应的表单-->
                    <form action="javascript:" method="post">
                        <input type="text" class="search-text" v-model="keyword" v-on:focus="focus" v-on:blur="randomLabel" />
                        <input type="submit" value="搜索" class="search-button" @click="goSearch" />
                    </form>
                </div>
                <div class="search-item clearfix" @click="goCommend">
                    <div style="float: left">热门搜索：</div>
                    <a v-for="(item,index) in commendList" :key="index" :data-commendname="item">{{ item }}</a>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            commendList: ["水果", "电脑", "手机", "苹果"],
            keyword: "",
        };
    },
    mounted() {
        this.randomLabel();
    },
    methods: {
        focus() {
            this.keyword = "";
        },
        randomLabel() {
            this.keyword =
                this.keyword ||
                this.commendList[
                    parseInt(Math.random() * this.commendList.length)
                ];
        },
        goSearch() {
            event.preventDefault();
            if (!this.keyword.trim()) {
                this.$message({
                    message: "搜索内容不能为空",
                    type: "warning",
                });
                return;
            }
            this.$router.push({
                name: "search",
                params: {
                    keyword: this.keyword,
                },
            });
        },
        goCommend(event) {
            let element = event.target;
            if (element.dataset.commendname) {
                this.$router.push({
                    name: "search",
                    params: {
                        keyword: element.dataset.commendname,
                    },
                });
            }
        },
    },
};
</script>

<style lang="less" scoped>
.header {
    width: 100%;
    height: 120px;
    background-color: #fff;

    .box1 {
        width: 1200px;
        margin: 0 auto;
    }

    .logo {
        margin-top: 32px;
        float: left;
    }

    a img {
        display: block;
        width: 180px;
        height: 50px;
    }

    .box2 {
        float: left;
        margin-left: 121px;
        margin-top: 30px;
        width: 600px;
        height: 90px;

        .search {
            background-color: rgb(232, 44, 41);
        }

        form {
            .search-text:focus {
                color: #3f3f3f;
            }

            .search-text {
                float: left;
                width: 476px;
                height: 20px;
                padding: 8px 8px 8px 14px;
                border: 2px solid rgb(232, 44, 41);
                border-right: 0;
                color: #969ba0;
                /*取消文本框被选中时出现的边框（实际上是轮廓）*/
                outline: none;
            }

            .search-button {
                float: left;
                width: 100px;
                height: 40px;
                border: none;
                background-color: rgb(232, 44, 41);
                font-size: 20px;
                color: white;
                line-height: 40px;
                padding: 0;
            }
        }

        .search-item {
            margin-top: 8px;
            color: #666;

            a {
                text-decoration: none;
                color: #666;
                float: left;
                margin-left: 8px;

                &:hover {
                    color: red;
                    text-decoration: underline;
                    // 让鼠标变成小手
                    cursor: pointer;
                }
            }
        }
    }
}
</style>